package com.tjsinfo.tjsoa.documentationflow.dangweifawen.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.bo.ITDangweifawenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenNeirong;
//import com.tjsinfo.tjsoa.documentationflow.tanweifawen.bo.ITTuanweifaweiNeirongBO;
//import com.tjsinfo.tjsoa.documentationflow.tanweifawen.vo.TTuanweifaweiNeirong;
import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workflow.bo.ITaskInstanceBO;

public class JingShouRenSelectAction  extends Action{
	private ITDangweifawenNeirongBO dangweifawenNeirongBO;
	private ISystemUserBo systemUserBo;
	private IDepartmentBo departmentBo;
	private ISystemUserDepartmentBo systemUserDepartmentBo;
	private ITaskInstanceBO taskInstanceBO;
	
	
	public ITaskInstanceBO getTaskInstanceBO() {
		return taskInstanceBO;
	}
	public void setTaskInstanceBO(ITaskInstanceBO taskInstanceBO) {
		this.taskInstanceBO = taskInstanceBO;
	}
	public IDepartmentBo getDepartmentBo() {
		return departmentBo;
	}
	public void setDepartmentBo(IDepartmentBo departmentBo) {
		this.departmentBo = departmentBo;
	}
	public ISystemUserDepartmentBo getSystemUserDepartmentBo() {
		return systemUserDepartmentBo;
	}
	public void setSystemUserDepartmentBo(
			ISystemUserDepartmentBo systemUserDepartmentBo) {
		this.systemUserDepartmentBo = systemUserDepartmentBo;
	}
	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}
	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}




	public ITDangweifawenNeirongBO getDangweifawenNeirongBO() {
		return dangweifawenNeirongBO;
	}





	public void setDangweifawenNeirongBO(
			ITDangweifawenNeirongBO dangweifawenNeirongBO) {
		this.dangweifawenNeirongBO = dangweifawenNeirongBO;
	}





	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		Boolean isZhiShu = false;
		Integer first = 0;
		Integer state = Integer.parseInt(request.getParameter("state")); 
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		List<TDangweifawenNeirong> list;
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		criteria.add(Restrictions.eq("state", state));
		
		
		if(request.getParameter("zhaiyao") != null && !"".equals(request.getParameter("zhaiyao"))){
			criteria.add(Restrictions.like("zhaiyao", "%"+request.getParameter("zhaiyao")+"%"));
		}
		if(request.getParameter("wenhao") != null && !"".equals(request.getParameter("wenhao"))){
			criteria.add(Restrictions.like("wenhaotype", "%"+request.getParameter("wenhao")+"%"));
		}
		
		String zhubuzhang = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource_zh_CN.properties", this)
		.getProperty("xingzhengshouwen.zhubuzhang");
		if (systemUserBo.isUserDuty(SessionUser.getUserId(request), zhubuzhang)) {
			Disjunction or = Restrictions.disjunction();
			if(SessionUser.getUser(request).getDepartment() != null){
				String department[] = SessionUser.getUser(request).getDepartment().split(",");
				for (String departmentid : department) {
					if(departmentid !=null &&!"".equals(departmentid)){
						or.add(Restrictions.like("jingshoubumen", "%,"+departmentid+",%"));
					}
				}
			}
			criteria.add(or.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%")));
		} 
		else{
			criteria.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%"));
		}
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		if (request.getParameter("start") == null) {
			list =  dangweifawenNeirongBO.selectDangweifawenPageCriteria(criteria, 0, 23);
		} else {
			first = Integer.parseInt(request.getParameter("start"));
			list = dangweifawenNeirongBO.selectDangweifawenPageCriteria(criteria,first, 23);
		}
		criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		criteria.add(Restrictions.eq("state", state));

		 zhubuzhang = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource_zh_CN.properties", this)
		.getProperty("xingzhengshouwen.zhubuzhang");
		if (systemUserBo.isUserDuty(SessionUser.getUserId(request), zhubuzhang)) {
			Disjunction or = Restrictions.disjunction();
			if(SessionUser.getUser(request).getDepartment() != null){
				String department[] = SessionUser.getUser(request).getDepartment().split(",");
				for (String departmentid : department) {
					if(departmentid !=null &&!"".equals(departmentid)){
						or.add(Restrictions.like("jingshoubumen", "%,"+departmentid+",%"));
					}
				}
			}
			criteria.add(or.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%")));
		} 
		else{
			criteria.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%"));
		}
		Integer totalProperty = dangweifawenNeirongBO.selectDangweifawenTotalLineCriteria(criteria);
		object.put("totalProperty", totalProperty);
		for (TDangweifawenNeirong tuanweifaweiNeirong : list) {
			String nodename = workFlowTemplate.selectNodeName(Long.valueOf(tuanweifaweiNeirong.getFlowid().toString()));
			JSONObject zuzhibufaweiNeirongJSON = new JSONObject();
			zuzhibufaweiNeirongJSON.put("zhaiyao", tuanweifaweiNeirong.getBiaoti());
			zuzhibufaweiNeirongJSON.put("zhusong", tuanweifaweiNeirong.getZhusong());
			zuzhibufaweiNeirongJSON.put("chaosong", tuanweifaweiNeirong.getChaosong());
			zuzhibufaweiNeirongJSON.put("fujian", tuanweifaweiNeirong.getFujian());
			if(tuanweifaweiNeirong.getWenhaotype() != null )
    		{
    			String webnumber = tuanweifaweiNeirong.getWenhaotype().replace("[","〔");
    			webnumber = webnumber.replace("]","〕");
    			zuzhibufaweiNeirongJSON.put("wenhao", webnumber);
    		}else{
    			zuzhibufaweiNeirongJSON.put("wenhao", "");
    		}	
			List<TSystemUserDepartment> systemUserDepartmentList = systemUserDepartmentBo.selectByUserId(SessionUser.getUser(request).getUser_id());
			for (TSystemUserDepartment systemUserDepartment : systemUserDepartmentList) {
				String departmentId = systemUserDepartment.getDepartmentId();
				TSystemDepartment systemDepartment = departmentBo.findById(departmentId);
				if(systemDepartment.getIsZhiShu() != null){
					isZhiShu = true;
				}
			}
			if(isZhiShu  == true){
				zuzhibufaweiNeirongJSON.put("operate", "<a href='"+basePath+"tjsinfo/tjsoa/dangweifawen/shenpi.do?method=selectonedoc&id="+tuanweifaweiNeirong.getId()+"'>查看</a>");
			}else{
				zuzhibufaweiNeirongJSON.put("operate", "<a href='"+basePath+"documentationflow/dangweifawen/genzong.jsp?flowid="+tuanweifaweiNeirong.getFlowid()+
						"'>跟踪</a>&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/dangweifawen/shenpi.do?method=selectonedoc&id="+tuanweifaweiNeirong.getId()+"'>查看</a>"+
						"&nbsp;&nbsp;<a href='"+basePath+"documentationflow/dangweifawen/fubenlist.jsp?id="+tuanweifaweiNeirong.getId()+"'>查看副本</a>");
			}
			
			if ("停止".equals(nodename)){
				zuzhibufaweiNeirongJSON.put("state", "是");
			}
			else if (workFlowTemplate.isSuspend(Long.parseLong(tuanweifaweiNeirong.getFlowid().toString()) )){
				zuzhibufaweiNeirongJSON.put("state", "中止");
			}
			else{
				zuzhibufaweiNeirongJSON.put("state", "否");
			}
			String node = taskInstanceBO.selectNodeByProcessId(tuanweifaweiNeirong.getFlowid());
			if ("办公室主任复核".equals(node)) {
				zuzhibufaweiNeirongJSON.put("node", "党办复核");
			}else{
				zuzhibufaweiNeirongJSON.put("node", node);
			}
			

			array.put(zuzhibufaweiNeirongJSON);
		}
		request.getSession().setAttribute("dangweifajingshourenliststart", first);
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	
}
